package tools;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by TJ on 2016/3/4.
 */
public class TimeTools {
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat(DEFAULT_PATTERN);

    public static String startOfDay(Calendar c) {
        if (c == null) return null;
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return calendar2String(c);
    }

    public static Calendar startOfDay(Calendar c, Object placeholder) {
        if (c == null) return null;
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    public static String endOfDay(Calendar c) {
        if (c == null) return null;
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 998);
        return calendar2String(c);
    }

    public static Calendar endOfDay(Calendar c, Object placeholder) {
        if (c == null) return null;
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 998);
        return c;
    }

    public static String calendar2String(Calendar c) {
        if (c == null) return null;
        return DEFAULT_FORMAT.format(c.getTime());
    }

    public static Date string2Date(String s) throws ParseException {
        if (s == null || s.length() == 0) return null;
        return DEFAULT_FORMAT.parse(s);
    }

    public static String getCurrentTime() {
        return new SimpleDateFormat(DEFAULT_PATTERN).format(new Date());
    }
    public static Calendar string2Calendar(String s) throws ParseException {
        return string2Calendar(s, DEFAULT_FORMAT);
    }
    public static String calendar2String(Calendar c, DateFormat format) {
        if (c == null)
            return null;
        return (format == null ? DEFAULT_FORMAT : format)
                .format(c.getTime());
    }

    public static Calendar string2Calendar(String s, DateFormat format)
            throws ParseException {
        if (s.equals("")||s.isEmpty())
            return null;
        Calendar c = Calendar.getInstance();
        format = (format == null ? DEFAULT_FORMAT : format);
        format.setLenient(false);
        Date date = format.parse(s);
        c.setTimeInMillis(date.getTime());
        return c;
    }

    /**
     * 计算某时间到当前时间的间隔分钟数
     * @param fromTime
     * @return
     */
    public static Integer intervalMinutes(String fromTime) {
        try {
            Date from = string2Date(fromTime);
            if (from == null) return null;
            BigDecimal gd = new BigDecimal((new Date().getTime() - from.getTime())/60000);
            return gd.setScale(0, RoundingMode.HALF_UP).abs().intValue();
        } catch (ParseException e) {
            return null;
        }
    }

    public static  long timingTen(int time){
        Calendar calendar = Calendar.getInstance();
//        calendar.set(Calendar.DAY_OF_MONTH,);
        calendar.set(Calendar.HOUR_OF_DAY,time);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return  calendar.getTimeInMillis();
    }
}
